/*
 * @Author: zt 2993721164@qq.com
 * @Date: 2025-08-06 20:26:06
 * @LastEditors: zt 2993721164@qq.com
 * @LastEditTime: 2025-08-07 01:50:25
 * @FilePath: /yyshort/lib/pages/ali_player/short_video/short_video/view.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:preload_page_view/preload_page_view.dart';
import 'package:yyshort/pages/ali_player/short_video/short_video/short_video_item.dart';
import 'controller.dart';

/// 短视频页面视图
class ShortVideoPageView extends GetView<ShortVideoController> {
  const ShortVideoPageView({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Obx(() {
        if (controller.state.isLoading.value) {
          return Container(
            color: Colors.black,
            alignment: Alignment.center,
            child: const CircularProgressIndicator(),
          );
        }
        return _buildShortVideoList();
      }),
    );
  }

  /// 构建短视频列表
  Widget _buildShortVideoList() {
    // 根据 preload 参数选择使用 PreloadPageView 或 PageView
    Widget pageView = controller.state.preload
        ? PreloadPageView.builder(
            controller: controller.state.preloadPageController,
            scrollDirection: Axis.vertical,
            itemCount: controller.state.videoInfoList.length,
            itemBuilder: _buildItem,
            preloadPagesCount: 1,
          )
        : PageView.builder(
            controller: controller.state.pageController,
            scrollDirection: Axis.vertical,
            itemCount: controller.state.videoInfoList.length,
            itemBuilder: _buildItem,
          );

    // 组合页面视图
    return Stack(
      children: [
        // 视频页面视图
        pageView,

        // 可以在这里添加其他UI元素，如信息卡片等
      ],
    );
  }

  /// 构建列表项
  Widget _buildItem(BuildContext context, int index) {
    final videoItem = controller.state.videoInfoList[index];
    debugPrint("[ShortVideoPage][item][lifecycle][create][${videoItem.id}]");
    return ShortVideoItem(
      key: controller.state.itemKeys[index],
      controller: controller,
      autoPlay: index == controller.state.currentIndex.value,
      isforyouPage: controller.state.isforyouPage.value,
      videoInfo: videoItem,
    );
  }
}

// /// 沉浸式列表播放页面入口
// class ShortVideoPage extends StatelessWidget {
//   /// 是否启用预加载功能
//   final bool preload;

//   const ShortVideoPage({super.key, this.preload = true});

//   @override
//   Widget build(BuildContext context) {
//     return ShortVideoPageView();
//   }
// }
